#### Read-in ####
rm(list=ls())
library(tidyverse)
library(knitr)
library(statar)
library(rlang)

remotes::install_github("danmckinleythompson/doStata")
library(doStata)

# setwd("~/Dropbox/Project_fundraising/dataverse")

df <- readRDS("Modified Data/Response_with_CP.RDS")

Carnes  <- read.csv("Original Data/carnes_hansen_salary_replication.csv",stringsAsFactors = F)%>%
  filter(year == 2012)%>%select(state_ab,state_nm,total_comp)

NIMSP  <- read.csv("Original Data/NIMSP.csv",stringsAsFactors = F)
names(NIMSP) = tolower(names(NIMSP))

NIMSP <- NIMSP %>% 
  rename(year = election_year,state = election_jurisdiction, tot = total_.) %>% 
  mutate(type = ifelse(grepl("SENATE", office_sought), "Upper Chamber", "Lower Chamber"))%>%
  filter(incumbency_status %in% c("Open", "Challenger"),
         election_status == "Won-General",
         type=="Lower Chamber",
         year >= 2012) %>% 
  group_by(state) %>% 
  summarise(mean = mean(tot), 
            q25 = quantile(tot, .25),
            median = median(tot),
            q75 = quantile(tot, .75))%>%
  as.data.frame(.)

NIMSP <- NIMSP%>%select(state,mean)%>%
  rename(state_ab = state,average_fund = mean)
  
#### Merge to Carnes ####
df <- stata_merge(df,Carnes,by_vars =c("state_ab"),merge_type = "m:1",keep_cases = c(3))

#### Merge to NIMSP  ####
df <- stata_merge(df,NIMSP,by_vars =c("state_ab"),merge_type = "m:1",keep_cases = c(3))

#### Demographics (Type,Elected,PID_3,Gender,Level,State_code,Population,Urban_prop,ideo5,gov_exp,Born,White_nonhispanic,Education)#####
df$ideo5 = car::recode(df$ideo5, 
                        "'Very liberal' = 1;
                        'Somewhat liberal' = 2;
                        'Moderate, middle of the road' = 3;
                        'Somewhat conservative' = 4;
                        'Very conservative' = 5;
                        else = 3")

# age
df$age = 2017 - as.numeric(df$Born)
df$age =  ifelse(df$age>median(df$age,na.rm = T), "Older","Younger")

# college
df$college <- ifelse(df$Education %in% c("Some graduate school","Graduate degree","College graduate"),1,0)
df$college <- ifelse(df$Education %in% c(""),NA,df$college)


#### Interest variable ####
df$run_interest = NA_character_
df$run_interest[df$interest != ""] = df$interest[df$interest != ""]
df$run_interest[is.na(df$run_interest) & df$interest.1 != ""] = df$interest.1[is.na(df$run_interest) & df$interest.1 != ""]
df$run_interest[is.na(df$run_interest) & df$interest.2 != ""] = df$interest.2[is.na(df$run_interest) & df$interest.2 != ""]
df$run_interest[grep("I have no interest", df$run_interest)] = "No interest"
df$run_interest[grep("I am open to the possibility", df$run_interest)] = "Open to possibility"
df$run_interest[grep("I am actively considering", df$run_interest)] = "Actively considering"
df$interest = NULL
df$interest.1 = NULL
df$interest.2 = NULL

#### Fundraising variable #####
# recode fundraising to be midpoint of bin
fund_recodes = "'I did not raise any money during my last campaign'  = 0;
'$5,000 – 10,000' = 7.5;
'$1,000 – 5,000' = 3;
'Under $1,000' = 1;
'$20,000 – 50,000' = 30;
'$10,000 – 20,000' = 15;
'$100,000 – 250,000' = 175;
'$50,000 – 75,000' = 67.5;
'$75,000 – 100,000' = 87.5;
'$250,000 – 500,000' = 375;
'Over $500,000' = 500;
'' = NA"
df = df %>% 
  mutate(fund_amount_num = car::recode(fund_amount, fund_recodes))

#### Export ######
df %>%saveRDS(.,"Modified Data/Clean.RDS")
